/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Kampania_Kontroler.java
 *
 * Created on 2010-06-03, 16:58:03
 */

package adbus;

import adbus.models.*;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *klasa odpowiedzialna z wyswietlanie okna dodania kampani
 * i obsluge zdarzen z nim zwiazanych
 * @author rafal
 */
public class Kampania_Kontroler extends javax.swing.JDialog {
    private Kampania campaign = new Kampania();
    private KampaniaTable campaignTable = null;
    private newAdvert newAdvertBox = null;

    /** Creates new form Kampania_Kontroler */
    public Kampania_Kontroler(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
/**dwuargumentowy konstruktor klasy Kampania_Kontener*/
    public Kampania_Kontroler(JFrame mainFrame, KampaniaTable kmTable) {
        super(mainFrame,true);
        initComponents();
        campaignTable = kmTable;
    }
    /**trzyargumentowy konstruktor klasy Kampania_Kontener*/
    public Kampania_Kontroler(JFrame mainFrame, KampaniaTable kmTable, Kampania kamp) {
        super(mainFrame,true);
        campaign = kamp;
        campaign.zaladujReklamy();
        initComponents();
        campaignTable = kmTable;
        Klient k = new Klient();
        k.pobierzWgId(campaign.id_klient);
        clientsList.getModel().setSelectedItem(k);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        campaignNameLabel = new javax.swing.JLabel();
        campaignName = new javax.swing.JTextField();
        clientLabel = new javax.swing.JLabel();
        clientsList = new javax.swing.JComboBox();
        advertsLabel = new javax.swing.JLabel();
        addAdvertButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        advertList = new javax.swing.JList();
        removeAdvertButton = new javax.swing.JButton();
        timeLabel = new javax.swing.JLabel();
        dateFrom = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        dateTo = new javax.swing.JTextField();
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(adbus.AdBusApp.class).getContext().getResourceMap(Kampania_Kontroler.class);
        campaignNameLabel.setText(resourceMap.getString("campaignNameLabel.text")); // NOI18N
        campaignNameLabel.setName("campaignNameLabel"); // NOI18N

        campaignName.setText(campaign.nazwa);
        campaignName.setName("campaignName"); // NOI18N

        clientLabel.setText(resourceMap.getString("clientLabel.text")); // NOI18N
        clientLabel.setName("clientLabel"); // NOI18N

        clientsList.setModel(new adbus.models.KlientList());
        clientsList.setName("clientsList"); // NOI18N

        advertsLabel.setText(resourceMap.getString("advertsLabel.text")); // NOI18N
        advertsLabel.setName("advertsLabel"); // NOI18N

        addAdvertButton.setText(resourceMap.getString("addAdvertButton.text")); // NOI18N
        addAdvertButton.setName("addAdvertButton"); // NOI18N
        addAdvertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAdvertButtonActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        advertList.setModel(campaign.adverts);
        advertList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        advertList.setName("advertList"); // NOI18N
        jScrollPane1.setViewportView(advertList);

        removeAdvertButton.setText(resourceMap.getString("removeAdvertButton.text")); // NOI18N
        removeAdvertButton.setName("removeAdvertButton"); // NOI18N
        removeAdvertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeAdvertButtonActionPerformed(evt);
            }
        });

        timeLabel.setText(resourceMap.getString("timeLabel.text")); // NOI18N
        timeLabel.setName("timeLabel"); // NOI18N

        dateFrom.setText(campaign.data_rozpoczecia);
        dateFrom.setName("dateFrom"); // NOI18N
        dateFrom.setPreferredSize(new java.awt.Dimension(88, 27));

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        dateTo.setText(campaign.data_zakonczenia);
        dateTo.setName("dateTo"); // NOI18N
        dateTo.setPreferredSize(new java.awt.Dimension(88, 27));

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(campaignNameLabel)
                    .addComponent(clientLabel)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(addAdvertButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(removeAdvertButton))
                        .addComponent(advertsLabel, javax.swing.GroupLayout.Alignment.LEADING))
                    .addComponent(timeLabel))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(clientsList, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(campaignName, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                            .addComponent(cancelButton, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(okButton, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(dateFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dateTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campaignNameLabel)
                    .addComponent(campaignName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(clientLabel)
                    .addComponent(clientsList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(timeLabel)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(dateFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5)
                        .addComponent(dateTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(advertsLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(addAdvertButton)
                            .addComponent(removeAdvertButton)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
/**metoda odpowiedzialna za obsluge przycisku ok*/
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        campaign.nazwa = campaignName.getText();
        campaign.zapiszDate(dateFrom.getText(), dateTo.getText());
        campaign.przypiszKlienta((Klient)clientsList.getSelectedItem());
        campaign.zapisz();
        dispose();
        if(campaignTable!=null) {
            campaignTable.zaladuj();
        }
    }//GEN-LAST:event_okButtonActionPerformed
/**metoda odpowiedzialna za obsluge przycisku anuluj*/
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed
/**metoda odpowiedzialna za obsluge przycisku dodania reklamy*/
    private void addAdvertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAdvertButtonActionPerformed
        if(!validData())
            return;
        campaign.zapiszDate(dateFrom.getText(), dateTo.getText());
        campaign.nazwa = campaignName.getText();
        if(campaign.pobierzDostepnychPrzewoznikow().getSize()==0) {
            JOptionPane.showMessageDialog(this, "Nie ma żadnych dostępnych pojazdów w tym czasie!", "Ostrzeżenie", JOptionPane.WARNING_MESSAGE);
            return;
        }
        campaign.pobierzDostepneLinie();
        campaign.pobierzDostepnePojazdy();
        if (newAdvertBox == null) {
            JFrame mainFrame = AdBusApp.getApplication().getMainFrame();
            newAdvertBox = new newAdvert(mainFrame,campaign);
            newAdvertBox.setSize(newAdvertBox.getPreferredSize());
            newAdvertBox.setLocationRelativeTo(mainFrame);
        }

        //newAdvertBox.reload();
        if(((Pojazd)newAdvertBox.getPojazdList().getSelectedItem())!=null)
            ((Pojazd)newAdvertBox.getPojazdList().getSelectedItem()).pobierzFormaty();
        
        AdBusApp.getApplication().show(newAdvertBox);
    }//GEN-LAST:event_addAdvertButtonActionPerformed
/**metoda odpowiedzialna za obsluge przycisku usuniecia reklamy*/
    private void removeAdvertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeAdvertButtonActionPerformed
        for(Object o: campaign.adverts.toArray()) {
            //campaign.adverts.
            if(!advertList.isSelectionEmpty()) {
                campaign.adverts.remove(advertList.getSelectedIndex());
                campaign.setToDelete((Reklama)advertList.getSelectedValue());
            }
        }
    }//GEN-LAST:event_removeAdvertButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Kampania_Kontroler dialog = new Kampania_Kontroler(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addAdvertButton;
    private javax.swing.JList advertList;
    private javax.swing.JLabel advertsLabel;
    private javax.swing.JTextField campaignName;
    private javax.swing.JLabel campaignNameLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel clientLabel;
    private javax.swing.JComboBox clientsList;
    private javax.swing.JTextField dateFrom;
    private javax.swing.JTextField dateTo;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okButton;
    private javax.swing.JButton removeAdvertButton;
    private javax.swing.JLabel timeLabel;
    // End of variables declaration//GEN-END:variables
/**metoda odpowiedzialna za wyswietlanie ostrzezen*/
    private boolean validData() {
        if(campaignName.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Kampania musi mieć nazwę","Ostrzeżenie",JOptionPane.WARNING_MESSAGE);
            return false;
        }
        if(dateFrom.getText().equals("") || dateTo.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Najpierw wpisz datę kampanii!","Ostrzeżenie",JOptionPane.WARNING_MESSAGE);
            return false;
        } else {
            //sprawdzanie poprawności daty
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            ParsePosition pos = new ParsePosition(0);
            Date from = df.parse(dateFrom.getText(), pos);
            if(from == null) {
                JOptionPane.showMessageDialog(this, "Data początkowa jest nieprawdłowa - prawidłowy format to RRRR-MM-DD","Ostrzeżenie",JOptionPane.WARNING_MESSAGE);
                return false;
            }
            pos.setIndex(0);
            Date dateto = df.parse(dateTo.getText(),pos);
            if(dateto == null) {
                JOptionPane.showMessageDialog(this, "Data końcowa jest nieprawdłowa - prawidłowy format to RRRR-MM-DD","Ostrzeżenie",JOptionPane.WARNING_MESSAGE);
                return false;
            }
            if(!dateto.after(from)) {
                JOptionPane.showMessageDialog(this, "Data końcowa musi być po dacie początkowej!","Ostrzeżenie",JOptionPane.WARNING_MESSAGE);
                return false;
            }
            return true;
        }
    }
    //private Kampania campaign = new Kampania();
}
